package constant.milk.cashbook.util;

import java.util.ArrayList;
import java.util.Calendar;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;


import constant.milk.cashbook.main.StartDateActivitiy;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.TextView;

public class CalendarActivity extends SuperActivity implements OnClickListener
{
	public final static String START_DATE_FLAG = "START_DATE_FLAG";
	
	public final static String YEAR = "CALENDAR_YEAR";
	public final static String MONTN = "CALENDAR_MONTN";
	public final static String DAY = "CALENDAR_DAY";

	private static final int REQUEST_CALENDER = 0;
	
	private ImageButton pre, next;
	private TextView    today;
	
	private TextView sunday, saturday;
	
	private TextView calendar1_0;
	private TextView calendar1_1;
	private TextView calendar1_2;
	private TextView calendar1_3;
	private TextView calendar1_4;
	private TextView calendar1_5;
	private TextView calendar1_6;
	
	private TextView calendar2_0;
	private TextView calendar2_1;
	private TextView calendar2_2;
	private TextView calendar2_3;
	private TextView calendar2_4;
	private TextView calendar2_5;
	private TextView calendar2_6;
	
	private TextView calendar3_0;
	private TextView calendar3_1;
	private TextView calendar3_2;
	private TextView calendar3_3;
	private TextView calendar3_4;
	private TextView calendar3_5;
	private TextView calendar3_6;

	private TextView calendar4_0;
	private TextView calendar4_1;
	private TextView calendar4_2;
	private TextView calendar4_3;
	private TextView calendar4_4;
	private TextView calendar4_5;
	private TextView calendar4_6;
	
	private TextView calendar5_0;
	private TextView calendar5_1;
	private TextView calendar5_2;
	private TextView calendar5_3;
	private TextView calendar5_4;
	private TextView calendar5_5;
	private TextView calendar5_6;
	
	private TextView calendar6_0;
	private TextView calendar6_1;
	private TextView calendar6_2;
	private TextView calendar6_3;
	private TextView calendar6_4;
	private TextView calendar6_5;
	private TextView calendar6_6;
	
	private ArrayList<TextView> tempList = new ArrayList<TextView>();
	
	private int setYear;
	private int setMonth;
	private int setDay;
	
	private Button startButton;
	private Button checkButton;
	
	private int colorSelect;
	
	private int toYear;
	private int toMonth;
	private int toDay;
	
	private int startFlag;
	
	private int startDate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.calendar_activity);
		
		setTitle("날짜선택");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	@Override
	protected void setFindViewId()
	{
		pre = (ImageButton) findViewById(R.id.calendar_picker_pre);
		next = (ImageButton) findViewById(R.id.calendar_picker_next);
		today = (TextView) findViewById(R.id.calendar_picker_today);
		
		sunday = (TextView) findViewById(R.id.calendarSunday);
		saturday = (TextView) findViewById(R.id.calendarSaturday);
		
		calendar1_0 = (TextView) findViewById(R.id.calendar1_0);
		calendar1_1 = (TextView) findViewById(R.id.calendar1_1);
		calendar1_2 = (TextView) findViewById(R.id.calendar1_2);
		calendar1_3 = (TextView) findViewById(R.id.calendar1_3);
		calendar1_4 = (TextView) findViewById(R.id.calendar1_4);
		calendar1_5 = (TextView) findViewById(R.id.calendar1_5);
		calendar1_6 = (TextView) findViewById(R.id.calendar1_6);
		
		calendar2_0 = (TextView) findViewById(R.id.calendar2_0);
		calendar2_1 = (TextView) findViewById(R.id.calendar2_1);
		calendar2_2 = (TextView) findViewById(R.id.calendar2_2);
		calendar2_3 = (TextView) findViewById(R.id.calendar2_3);
		calendar2_4 = (TextView) findViewById(R.id.calendar2_4);
		calendar2_5 = (TextView) findViewById(R.id.calendar2_5);
		calendar2_6 = (TextView) findViewById(R.id.calendar2_6);
		
		calendar3_0 = (TextView) findViewById(R.id.calendar3_0);
		calendar3_1 = (TextView) findViewById(R.id.calendar3_1);
		calendar3_2 = (TextView) findViewById(R.id.calendar3_2);
		calendar3_3 = (TextView) findViewById(R.id.calendar3_3);
		calendar3_4 = (TextView) findViewById(R.id.calendar3_4);
		calendar3_5 = (TextView) findViewById(R.id.calendar3_5);
		calendar3_6 = (TextView) findViewById(R.id.calendar3_6);
		
		calendar4_0 = (TextView) findViewById(R.id.calendar4_0);
		calendar4_1 = (TextView) findViewById(R.id.calendar4_1);
		calendar4_2 = (TextView) findViewById(R.id.calendar4_2);
		calendar4_3 = (TextView) findViewById(R.id.calendar4_3);
		calendar4_4 = (TextView) findViewById(R.id.calendar4_4);
		calendar4_5 = (TextView) findViewById(R.id.calendar4_5);
		calendar4_6 = (TextView) findViewById(R.id.calendar4_6);
		
		calendar5_0 = (TextView) findViewById(R.id.calendar5_0);
		calendar5_1 = (TextView) findViewById(R.id.calendar5_1);
		calendar5_2 = (TextView) findViewById(R.id.calendar5_2);
		calendar5_3 = (TextView) findViewById(R.id.calendar5_3);
		calendar5_4 = (TextView) findViewById(R.id.calendar5_4);
		calendar5_5 = (TextView) findViewById(R.id.calendar5_5);
		calendar5_6 = (TextView) findViewById(R.id.calendar5_6);
		
		calendar6_0 = (TextView) findViewById(R.id.calendar6_0);
		calendar6_1 = (TextView) findViewById(R.id.calendar6_1);
		calendar6_2 = (TextView) findViewById(R.id.calendar6_2);
		calendar6_3 = (TextView) findViewById(R.id.calendar6_3);
		calendar6_4 = (TextView) findViewById(R.id.calendar6_4);
		calendar6_5 = (TextView) findViewById(R.id.calendar6_5);
		calendar6_6 = (TextView) findViewById(R.id.calendar6_6);
		
		startButton = (Button) findViewById(R.id.calendarStartDateButton);
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		pre.setOnClickListener(CalendarActivity.this);
		next.setOnClickListener(CalendarActivity.this);
		
		today.setOnClickListener(CalendarActivity.this);
		
		calendar1_0.setOnClickListener(CalendarActivity.this);
		calendar1_1.setOnClickListener(CalendarActivity.this);
		calendar1_2.setOnClickListener(CalendarActivity.this);
		calendar1_3.setOnClickListener(CalendarActivity.this);
		calendar1_4.setOnClickListener(CalendarActivity.this);
		calendar1_5.setOnClickListener(CalendarActivity.this);
		calendar1_6.setOnClickListener(CalendarActivity.this);
		
		calendar2_0.setOnClickListener(CalendarActivity.this);
		calendar2_1.setOnClickListener(CalendarActivity.this);
		calendar2_2.setOnClickListener(CalendarActivity.this);
		calendar2_3.setOnClickListener(CalendarActivity.this);
		calendar2_4.setOnClickListener(CalendarActivity.this);
		calendar2_5.setOnClickListener(CalendarActivity.this);
		calendar2_6.setOnClickListener(CalendarActivity.this);
		
		calendar3_0.setOnClickListener(CalendarActivity.this);
		calendar3_1.setOnClickListener(CalendarActivity.this);
		calendar3_2.setOnClickListener(CalendarActivity.this);
		calendar3_3.setOnClickListener(CalendarActivity.this);
		calendar3_4.setOnClickListener(CalendarActivity.this);
		calendar3_5.setOnClickListener(CalendarActivity.this);
		calendar3_6.setOnClickListener(CalendarActivity.this);
		
		calendar4_0.setOnClickListener(CalendarActivity.this);
		calendar4_1.setOnClickListener(CalendarActivity.this);
		calendar4_2.setOnClickListener(CalendarActivity.this);
		calendar4_3.setOnClickListener(CalendarActivity.this);
		calendar4_4.setOnClickListener(CalendarActivity.this);
		calendar4_5.setOnClickListener(CalendarActivity.this);
		calendar4_6.setOnClickListener(CalendarActivity.this);
		
		calendar5_0.setOnClickListener(CalendarActivity.this);
		calendar5_1.setOnClickListener(CalendarActivity.this);
		calendar5_2.setOnClickListener(CalendarActivity.this);
		calendar5_3.setOnClickListener(CalendarActivity.this);
		calendar5_4.setOnClickListener(CalendarActivity.this);
		calendar5_5.setOnClickListener(CalendarActivity.this);
		calendar5_6.setOnClickListener(CalendarActivity.this);
		
		calendar6_0.setOnClickListener(CalendarActivity.this);
		calendar6_1.setOnClickListener(CalendarActivity.this);
		calendar6_2.setOnClickListener(CalendarActivity.this);
		calendar6_3.setOnClickListener(CalendarActivity.this);
		calendar6_4.setOnClickListener(CalendarActivity.this);
		calendar6_5.setOnClickListener(CalendarActivity.this);
		calendar6_6.setOnClickListener(CalendarActivity.this);
		
		startButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, StartDateActivitiy.class);
				intent.putExtra(StartDateActivitiy.START_DATE_DATA, startDate + "");
				startActivityForResult(intent, REQUEST_CALENDER);
			}
		});
		
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(YEAR, toYear);
				intent.putExtra(MONTN, toMonth);
				intent.putExtra(DAY, toDay);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}
	
	private void setTextList()
	{
		tempList.add(calendar1_0);
		tempList.add(calendar1_1);
		tempList.add(calendar1_2);
		tempList.add(calendar1_3);
		tempList.add(calendar1_4);
		tempList.add(calendar1_5);
		tempList.add(calendar1_6);
		
		tempList.add(calendar2_0);
		tempList.add(calendar2_1);
		tempList.add(calendar2_2);
		tempList.add(calendar2_3);
		tempList.add(calendar2_4);
		tempList.add(calendar2_5);
		tempList.add(calendar2_6);
		
		tempList.add(calendar3_0);
		tempList.add(calendar3_1);
		tempList.add(calendar3_2);
		tempList.add(calendar3_3);
		tempList.add(calendar3_4);
		tempList.add(calendar3_5);
		tempList.add(calendar3_6);
		
		tempList.add(calendar4_0);
		tempList.add(calendar4_1);
		tempList.add(calendar4_2);
		tempList.add(calendar4_3);
		tempList.add(calendar4_4);
		tempList.add(calendar4_5);
		tempList.add(calendar4_6);
		
		tempList.add(calendar5_0);
		tempList.add(calendar5_1);
		tempList.add(calendar5_2);
		tempList.add(calendar5_3);
		tempList.add(calendar5_4);
		tempList.add(calendar5_5);
		tempList.add(calendar5_6);
	
		tempList.add(calendar6_0);
		tempList.add(calendar6_1);
		tempList.add(calendar6_2);
		tempList.add(calendar6_3);
		tempList.add(calendar6_4);
		tempList.add(calendar6_5);
		tempList.add(calendar6_6);
	}

	@Override
	protected void setData()
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		startDate = pref.getInt(getString(R.string.prefStartDate), 1);
		
		Intent intent = getIntent();
		startFlag = intent.getIntExtra(START_DATE_FLAG, 1);
		
		if (startFlag == 0)
		{
			startButton.setVisibility(View.VISIBLE);
		}
		else
		{
			startButton.setVisibility(View.GONE);
		}
		
		setTextList();
		
		sunday.setTextColor(Color.RED);
		saturday.setTextColor(Color.BLUE);
		
		calendar1_0.setTextColor(Color.RED);
		calendar1_6.setTextColor(Color.BLUE);
		
		calendar2_0.setTextColor(Color.RED);
		calendar2_6.setTextColor(Color.BLUE);
		
		calendar3_0.setTextColor(Color.RED);
		calendar3_6.setTextColor(Color.BLUE);
		
		calendar4_0.setTextColor(Color.RED);
		calendar4_6.setTextColor(Color.BLUE);
		
		calendar5_0.setTextColor(Color.RED);
		calendar5_6.setTextColor(Color.BLUE);
		
		calendar6_0.setTextColor(Color.RED);
		calendar6_6.setTextColor(Color.BLUE);
		
		Calendar cal = Calendar.getInstance();
		
		toYear = intent.getIntExtra(YEAR, cal.get(Calendar.YEAR));
		toMonth = intent.getIntExtra(MONTN, cal.get(Calendar.MONTH));
		toDay = intent.getIntExtra(DAY, cal.get(Calendar.DAY_OF_MONTH));
		
		setYear = intent.getIntExtra(YEAR, cal.get(Calendar.YEAR));
		setMonth = intent.getIntExtra(MONTN, cal.get(Calendar.MONTH));
		
		colorSelect = getResources().getColor(R.color.selectCalendar);
		
		setCalendar(toYear, toMonth);
	}
	
	private void setCalendar(int year, int month) 
	{
		for (int i = 0; i < tempList.size(); i++)
		{
			TextView tv = (TextView) tempList.get(i);
			tv.setBackgroundColor(0);
			tv.setText("");
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, 1);

		if (startFlag == 0)
		{
			SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
			startDate = pref.getInt(getString(R.string.prefStartDate), 1);
			
			today.setText(year + "년 " + (month + 1) + "월 " + "(시작일 : " + startDate + ")");
		}
		else
		{
			today.setText(year + "년 " + (month + 1) + "월");
		}

		int getDay = calendar.get(Calendar.DAY_OF_WEEK);
		
		int j = 1;

		for (int i = getDay - 1; i < calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH) + getDay - 1; i++) 
		{
			TextView tv = tempList.get(i);
			
			if (j == toDay) 
			{
				if ((setYear == toYear) && (setMonth == toMonth)) 
				{
//					tv.setPaintFlags(tv.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
					tv.setBackgroundColor(colorSelect);
				}
				
				tv.setText(j + "");
			}
			else 
			{
				tv.setText(j + "");
			}
			
			j++;
		}
	}
	
	@Override
	public void onClick(View v) 
	{
		if (v.getId() == R.id.calendar_picker_pre) 
		{
			setMonth--;
			
			if (setMonth < 0) 
			{
				setYear--;
				setMonth = 11;
			}
			
			setCalendar(setYear, setMonth);
		} 
		else if (v.getId() == R.id.calendar_picker_next) 
		{
			setMonth++;
			
			if (setMonth > 11) 
			{
				setMonth = 0;
				setYear++;
			}
			
			setCalendar(setYear, setMonth);
		} 
		else if(v.getId() == R.id.calendar_picker_today)
		{
			DatePickerDialog dpd = new DatePickerDialog(superContext, new OnDateSetListener()
			{
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth)
				{
					Intent intent = new Intent();
					intent.putExtra(YEAR, year);
					intent.putExtra(MONTN, monthOfYear);
					intent.putExtra(DAY, dayOfMonth);
					setResult(Activity.RESULT_OK, intent);
					finish();
				}
			}, toYear, toMonth, toDay);
			
			dpd.show();
		}
		else 
		{
			toYear = setYear;
			toMonth = setMonth;
			
			for (int i = 0; i < tempList.size(); i++)
			{
				TextView tv = (TextView) tempList.get(i);
				tv.setBackgroundColor(0);
			}
			
			v.setBackgroundColor(colorSelect);
			toDay = Integer.parseInt(((TextView) v).getText().toString());
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_CALENDER)
		{
			setCalendar(setYear, setMonth);
		}
	}
	
	@Override
	protected void tipButton()
	{
	}
}
